<template>
    <div id="articleList" v-loading="loading">
      <div class="search">
        <el-input placeholder="请输入标题" style="margin-bottom: 20px"  v-model="searchText" @keyup.enter.native="searchArticle">
          <el-button slot="append" @click="searchArticle" icon="el-icon-search"></el-button>
        </el-input>
      </div>
      <!-- 导航 -->
      <div id="nav">
        <el-button @click="pageNum=1, getArticles(0)">全部</el-button>
        <el-button @click="pageNum=1, getArticles(item.id)" v-for="item in tags" :key="item.id">{{ item.tagName }}</el-button>
      </div>
      <ul class="item" v-for="article in articles">
        <li style="cursor: pointer" @click="$router.push('/article/'+article.id)">
          <div class="left" >
            <img :src="article.headUrl" style="width: 70px; height: 70px; border-radius: 100px"/>
          </div>
          <div class="right">
            <el-tag>{{ article.tagName }}</el-tag><span class="font14gray">{{ article.username }}</span>
            <span class="font14gray">发布时间 {{ article.createTime }}</span><br/>
            <span class="title">{{ article.title }}</span>
            <span class="icons"><i class="el-icon-thumb"/> {{ article.likes }}</span>
          </div>
        </li>
      </ul>
      <el-empty description="暂无帖子" v-if="this.articles.length == 0"/>
      <div class="pages">
        <el-pagination
            layout="prev, pager, next, total"
            :page-size="pageSize"
            :total="total"
            :current-page="pageNum"
            @current-change="handlePageChange"
        >
        </el-pagination>
      </div>
      <div id="inputs" v-if="this.isLogin===true && this.tagId!==0">
        <ArticleEditor @loadArticles="reload" :tagId="tagId"/>
      </div>
    </div>
</template>

<script>

import ArticleEditor from "../components/ArticleEditor";

export default {
  name: "ArticleList",
  props:["isLogin"],
  data(){
    return{
      tagId: 0,
      tags:[],
      articles:[],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      loading: false,
      title: '',
      searchText:''
    }
  },
  methods:{
    getArticles(id) {
      this.tagId = id;
      let param = {
        tagId : id,
        pageNum : this.pageNum,
        pageSize : this.pageSize,
        keyword: "",
        id: "",

      }
      this.loading = true;
      this.getRequest("article/",param).then(resp => {
        this.articles = resp.data.list
        this.pageNum = resp.data.pageNum
        this.total = resp.data.total
      })
      this.loading = false
    },
    handlePageChange(val){
      this.pageNum = val
      this.getArticles(this.tagId)
    },
    reload(val) {
      this.getArticles(val)
    },
    searchArticle(){
      let param = {
        tagId : "",
        pageNum : this.pageNum,
        pageSize : this.pageSize,
        keyword: this.searchText,
        id: "",
      }
      this.loading = true;
      this.getRequest("article/",param).then(resp => {
        this.articles = resp.data.list
        this.pageNum = resp.data.pageNum
        this.total = resp.data.total
      })
      this.loading = false
    }
  },
  components:{
    ArticleEditor
  },
  created() {
    this.loading = true;
    //获取tags
    this.getRequest("tag/list").then(resp => {
      if (resp.data) {
        this.tags = resp.data
      }
    })

    //默认拉取全部帖子
    this.getArticles(0)
    this.loading = false
  }
}
</script>

<style scoped>
li{
  display: block;
}
#nav{
  line-height: 60px;
  background-color: rgb(240, 240, 240);
  border-radius: 10px;
  padding-left: 30px;
  box-shadow: 0 0px 3px 0 rgba(0,0,0,.02), 0 4px 8px 0 rgba(0,0,0,.02);
  margin-bottom: 30px;
}

.left{
  float: left;
}
.item:first-child{
  margin-top: 0px;
}
.item{
  height: 80px;
  padding-bottom: 20px;
  border-bottom: 1px solid rgb(200, 200, 200);
  margin-top: 30px;
}
.right{
  padding-left: 100px;
}
.font14gray{
  display: inline-block;
  margin-left: 10px;
  font-size: 14px;
  color: rgb(152,152,152);
}
.title{
  display: block;
  margin-top: 10px;
  font-size: 18px;
}
.content{
  margin-top: 10px;
  display: block;
  font-size: 15px;
  color: rgb(170, 170, 170);
}

.pages{
  margin: 30px 0;
  float: right;
}

#inputs{
  clear: both;
}
.input_sub{
  margin-top: 20px;;
}
.icons{
  display: inline-block;
  float: right;
  font-size: 14px;
}
</style>